/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2011-11-1
/// Purpose: 		数据访问上下文类文件
/// ****************************************************************************
/// Modify By		Date			Remark
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RB.DataAccess.Configuration;
using System.Collections.Specialized;
using System.Data.Common;
using System.Diagnostics;

namespace RB.DataAccess
{
    /// <summary>
    /// 数据访问上下文类
    /// </summary>
    public abstract class DataContext
    {
        private static DataContext           _default        = null;
        private static DataContextCollection _dataContexts   = new DataContextCollection();


        private string _connectionString = null;

        public string ConnectionString
        {
            get { return _connectionString; }
        }

        protected DataContext()
        {
        }

        public virtual void Initialize(string connectionString, NameValueCollection parameters)
        {
            _connectionString = connectionString;
        }

        public static DataContext Default
        {
            get
            {
                // 当default为空时读取config文件中的设置，生成对象
                if (_default == null)
                {
                    _default = DataContextFactory.Create();
                }
                return _default;
            }
        }

        /// <summary>
        /// 返回DataContexts集合
        /// </summary>
        public static DataContextCollection DataContexts
        {
            get
            {
                return _dataContexts;
            }
        }

        /// <summary>
        /// 根据数据库连接串的Provider，创建查询器
        /// </summary>
        /// <returns>查询器实例</returns>
        public abstract Executor NewExecutor();

        public static Executor CreateExecutor()
        {
            return Default.NewExecutor();
        }

        #region abstract members to be overrided

        public abstract SqlEncoder NewSqlEncoder();

        /// <summary>
        /// 创建一个新的数据库连接，本方法用于类构造函数中，创建一个实例化的连接对象
        /// </summary>
        /// <returns>数据库连接类的实例</returns>
        public abstract DbConnection NewConnection();

        /// <summary>
        /// 创建一个新的数据库命令对象
        /// </summary>
        /// <returns></returns>
        public abstract DbCommand NewCommand();

        #endregion abstract members to be overrided

    }
}
